package com.scommunity.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scommunity.domain.Notice;
import com.scommunity.mapper.NoticeMapper;
import com.scommunity.service.INoticeService;
import com.scommunity.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper,Notice> implements INoticeService{

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private RedisUtils redisUtils;

//    @Cacheable(value = "noticeList")
    @Override
    public List<Notice> getList() {
        List<Notice> noticeList = redisUtils.get("noticeList",List.class);
        if (noticeList == null){
            noticeList = noticeMapper.selectList(null);
            redisUtils.set("noticeList",noticeList);
        }
        return noticeList;
    }

//    @CacheEvict(value = "noticeList")
    @Override
    public void update(Notice notice) {
        noticeMapper.updateById(notice);
        //更新缓存
        List<Notice> noticeList = noticeMapper.selectList(null);
        redisUtils.set("noticeList",noticeList);
    }

    @Override
    public void delete(Long noticeId) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_id",noticeId);
        noticeMapper.delete(queryWrapper);
        List<Notice> noticeList = noticeMapper.selectList(null);
        redisUtils.set("noticeList",noticeList);
    }
}
